<template>
  <div class="cakelist">
    <!-- 地址选项卡 -->
    <selectaddress />
    <!-- 侧边导航 -->
    <div class="sidebar">
      <van-sidebar v-model="select">
        <van-sidebar-item title="新品上市" />
        <van-sidebar-item title="经典甄选" />
        <van-sidebar-item title="萌宝之选" />
        <van-sidebar-item title="寿星双全" />
        <van-sidebar-item title="特色口味" />
        <van-sidebar-item title="青春专属" />
        <van-sidebar-item title="庆典时刻" />
      </van-sidebar>
    </div>
    <!-- 商品列表 -->
    <div class="item">
      <div class="goodsCard" v-for="item in goodslist" :key="item.id">
        <div @click="toDetail(item.id)">
          <div>
            <van-image
              width="4rem"
              height="4rem"
              fit="cover"
              :src="item.image"
            />
          </div>
          <div style="margin-left: 8px">
            <h4>{{ item.goods_name }}</h4>
            <span>¥{{ item.price }}</span>
          </div>
        </div>
        <div>
          <van-button
            size="mini"
            type="danger"
            icon="plus"
            round
            @click="pushCart(item)"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Selectaddress from "../components/Selectaddress.vue";
import { mapState } from "vuex";
import { mapMutations } from "vuex";
export default {
  components: { Selectaddress },
  data() {
    return {
      select: 0,
      goodslist: [],
    };
  },
  computed: {
    ...mapState(["cakeList"]),
  },
  methods: {
    ...mapMutations(["pushCart", "saveList"]),
    toDetail(id) {
      this.$router.push("/goodsdetail/" + id);
    },
    getGoodslist() {
      let url = "/shop/goodslist";
      this.axios
        .get(url)
        .then((res) => {
          console.log(res);
          this.goodslist = res.data.results;
          this.saveList({ type: "cakeList", list: this.goodslist });
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
  mounted() {
    if (this.cakeList.length == 0) {
      this.getGoodslist();
    } else {
      this.goodslist = this.cakeList;
    }
  },
  watch: {
    select() {
      if (this.cakeList.length != 0) {
        if (this.select == 0) {
          this.goodslist = this.cakeList;
        } else {
          this.goodslist = [];
          this.cakeList.forEach((item) => {
            if (item.category_id == this.select) {
              this.goodslist.push(item);
            }
          });
        }
      }
    },
  },
};
</script>

<style scoped>
.sidebar {
  float: left;
  height: calc(100vh - 180px);
  overflow-y: scroll;
}
.item {
  float: left;
  width: calc(100vw - 80px);
  height: calc(100vh - 180px);
  overflow-y: scroll;
}
.goodsCard {
  position: relative;
}
.goodsCard::after {
  content: "";
  display: block;
  clear: both;
}
.goodsCard > div,
.goodsCard > div > div {
  float: left;
}
.goodsCard > div > div > h4 {
  margin: 2px;
}
.goodsCard > div > div > span {
  color: #ee0a24;
}
.goodsCard > div:last-child {
  position: absolute;
  top: 50%;
  right: 25px;
  transform: translate(-50%, -50%);
}
</style>
